package com.supermali.creater;

import com.supermali.entity.npc.person.Person;
import com.supermali.listener.KeyEventSupport;
import com.supermali.listener.MouseSupport;
import com.supermali.system.SystemHelper;
import com.supermali.util.Constant;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * @project super-mali
 * @Date 2021/2/27
 * @Auth yangrui
 **/
public class GameStarter {

    private MapCreater creater;

    private PersonHelper personHelper;

    private JFrame frame ;
    // 游戏状态（特指游戏人物的运行状态）
    private GameStatu gameStatu;
    // 系统状态
    SystemHelper systemHelper;

    public enum GameStatu{
        Index,PreTips,Running,FinishMoveback,Stop
    }

    public GameStarter(JFrame frame) {
        this.frame = frame;
        this.init();
    }

    public void init(){
        creater = new MapCreater(this);
        this.personHelper = new PersonHelper(creater);
        gameStatu = GameStatu.Running;
    }

    public void show(Graphics graphics, long delta){
        if(gameStatu==GameStatu.FinishMoveback){
            creater.moveForworld(frame.getWidth(),frame.getHeight(), delta);
        }
        Person select = personHelper.select();

        creater.setPerson(select); // 把人物也加入到地图元素中
        creater.proccessData(delta);
        creater.show(graphics);
        if(select!=null){
            select.make(graphics);
        }
    }
    // 处理键盘事件
    public void proccessKey(long delta){
        // 非运行态，直接返回
        if(gameStatu!=GameStatu.Running){
            return;
        }
        Person select = personHelper.select();
        if(select==null) return;
        creater.setPerson(select);

        if(KeyEventSupport.getPressed(KeyEvent.VK_Q)==1){
            select.sendBullet(delta);
        }

        if(KeyEventSupport.getPressed(KeyEvent.VK_UP)!=0){
            if(select.getJumpBehavior().isOver()&&select.getGravity().isOver()) {
                select.getJumpBehavior().start();
            }
            if(KeyEventSupport.getPressed(KeyEvent.VK_RIGHT)!=0){
                select.getForwordBehavior().forward(delta);
                double x = select.getX();
                if(x* Constant.scale>Constant.FrameWidth/2){
                    creater.moveForworld(frame.getWidth(),frame.getHeight(), delta);
                }
            }
            if(KeyEventSupport.getPressed(KeyEvent.VK_LEFT)!=0){
                select.getGobackBehavior().goback(delta);
            }
        } else if(KeyEventSupport.getPressed(KeyEvent.VK_RIGHT)!=0){
            select.getForwordBehavior().forward(delta);
            double x = select.getX();
            if(x* Constant.scale>Constant.FrameWidth/2){
                creater.moveForworld(frame.getWidth(),frame.getHeight(),delta);
            }
        }else if(KeyEventSupport.getPressed(KeyEvent.VK_LEFT)!=0){
            select.getGobackBehavior().goback(delta);
        }else if(KeyEventSupport.getPressed(KeyEvent.VK_DOWN)!=0){
            select.getSquatDownBehavior().squat();
        }else {
            select.getTerminateBehavior().terminate();
        }
    }

    public void setFrame(JFrame frame) {
        this.frame = frame;
    }

    public void proccessMouse(){
        if(MouseSupport.isMouseClick()){
            MouseSupport.setMouseClick(false);
            frame.requestFocus();
        }
    }

    public void setGameStatu(GameStatu gameStatu) {
        this.gameStatu = gameStatu;
    }

    public GameStatu getGameStatu() {
        return gameStatu;
    }

    public void explainXml(String path){
        creater.explainXml(path);
        int width = frame.getContentPane().getWidth();
        int height = frame.getContentPane().getHeight();
        creater.loadDynamicMapFactor(width,height);
    }

    public void clear(){
        creater.clear();
        personHelper.setType(3);
    }

    public void setPerson(int type){
        personHelper.setType(type);
    }

    public SystemHelper getSystemHelper() {
        return systemHelper;
    }

    public void setSystemHelper(SystemHelper systemHelper) {
        this.systemHelper = systemHelper;
    }
}
